package cn.dzhang.rcp.hyperbola.adapters;

import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

import cn.dzhang.rcp.hyperbola.common.UIUtils;
import cn.dzhang.rcp.hyperbola.model.Alarm;
import cn.dzhang.rcp.hyperbola.model.ListHolder;

@SuppressWarnings("rawtypes")
public class AlarmAdapterFactory implements IAdapterFactory {
	private static class AlarmAdapter extends LabelProvider implements
			IWorkbenchAdapter4 {

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			if (element instanceof Alarm) {
				Alarm alarm = (Alarm) element;

				switch (columnIndex) {
				case 0:
					return "";
				case 1:
					return alarm.getName();
				case 2:
					return alarm.getLevel().toString();
				case 3:
					return alarm.getObject();
				case 4:
					return alarm.getDatetime().toString();
				case 5:
				default:
					return alarm.getDescription();
				}
			} else {
				return element.toString();
			}
		}

		@Override
		public Object[] getElements(Object inputElement) {
			if (inputElement instanceof Collection<?>) {
				Collection<?> collection = (Collection<?>) inputElement;
				return collection.toArray();
			} else if (inputElement instanceof ListHolder<?>) {
				ListHolder<?> listHolder = (ListHolder<?>) inputElement;
				return listHolder.getValue().toArray();
			}

			return new Object[0];
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			System.err
					.println("AlarmAdapterFactory.AlarmAdapter.inputChanged()");
		}

		@Override
		public Color getForeground(Object element, int columnIndex) {
			return null;
		}

		@Override
		public Color getBackground(Object element, int columnIndex) {
			if (element instanceof Alarm && columnIndex == 0) {
				Alarm alarm = (Alarm) element;
				return UIUtils.getColor(alarm);
			}

			return null;
		}

		@Override
		public Font getFont(Object element, int columnIndex) {
			return null;
		}
	}

	private static final AlarmAdapter adapter = new AlarmAdapter();

	public AlarmAdapterFactory() {
		System.err.println("AlarmAdapterFactory.AlarmAdapterFactory()");
	}

	@Override
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (IWorkbenchAdapter4.class == adapterType) {
			if (adaptableObject instanceof List<?>) {

				List<?> list = (List<?>) adaptableObject;
				if (!list.isEmpty() && Alarm.class.isInstance(list.get(0))) {
					return adapter;
				}
			} else if (adaptableObject instanceof ListHolder<?>) {
				ListHolder<?> listHolder = (ListHolder<?>) adaptableObject;
				List<?> list = listHolder.getValue();
				if (!list.isEmpty() && Alarm.class.isInstance(list.get(0))) {
					return adapter;
				}
			} else if (adaptableObject instanceof Alarm) {
				return adapter;
			}
		}

		return null;
	}

	@Override
	public Class[] getAdapterList() {
		return new Class<?>[] { IWorkbenchAdapter4.class };
	}

}
